﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SpeedTest
{
	public class Permissions
	{
		private bool view = false;
		private bool edit = false;
		private bool create = false;
		private bool delete = false;

		public bool View
		{
			get
			{
				return view;
			}
			set
			{
				view = value;
			}
		}

		public bool Edit
		{
			get
			{
				return edit;
			}
			set
			{
				edit = value;
			}
		}

		public bool Create
		{
			get
			{
				return create;
			}
			set
			{
				create = value;
			}
		}

		public bool Delete
		{
			get
			{
				return delete;
			}
			set
			{
				delete = value;
			}
		}

	}

	public class UserPermissions
	{
		private string userID = String.Empty;
		private Permissions release = new Permissions();
		private Permissions environment = new Permissions();
		private Permissions plan = new Permissions();
		private Permissions user = new Permissions();
		private Permissions role = new Permissions();
        private Permissions server = new Permissions();

		public string UserID
		{
			get
			{
				return userID;
			}
			set
			{
				userID = value;
			}
		}

		public Permissions Releases
		{
			get
			{
				return release;
			}
			set
			{
				release = value;
			}
		}

		public Permissions Environments
		{
			get
			{
				return environment;
			}
			set
			{
				environment = value;
			}
		}

		public Permissions Plans
		{
			get
			{
				return plan;
			}
			set
			{
				plan = value;
			}
		}

		public Permissions Users
		{
			get
			{
				return user;
			}
			set
			{
				user = value;
			}
		}

		public Permissions Roles
		{
			get
			{
				return role;
			}
			set
			{
				role = value;
			}
		}

        public Permissions Server
        {
            get
            {
                return server;
            }
            set
            {
                server = value;
            }
        }

		public bool ParsePermissions(DataTable permissionData)
		{
			if(permissionData.Rows.Count > 0)
			{
				//[ReleasePermissions], [EnvironPermissions], [PlanPermissions], [UserPermissions], [RolePermissions]
				release = ParsePermissionElement(permissionData.Rows[0]["ReleasePermissions"].ToString());
				environment = ParsePermissionElement(permissionData.Rows[0]["EnvironPermissions"].ToString());
				plan = ParsePermissionElement(permissionData.Rows[0]["PlanPermissions"].ToString());
				user = ParsePermissionElement(permissionData.Rows[0]["UserPermissions"].ToString());
				role = ParsePermissionElement(permissionData.Rows[0]["RolePermissions"].ToString());
                server = ParsePermissionElement(permissionData.Rows[0]["ServerOptPermissions"].ToString());
			}
			return true;
		}

		private Permissions ParsePermissionElement(string element)
		{
			Permissions permiss = new Permissions();
			element = element.ToUpper();

			if (element.Contains("V"))
			{
				permiss.View = true;
			}

			if (element.Contains("E"))
			{
				permiss.Edit = true;
			}

			if (element.Contains("C"))
			{
				permiss.Create = true;
			}

			if (element.Contains("D"))
			{
				permiss.Delete = true;
			}

			return permiss;

		}
	}
}
